package com.zhonghui.common.core.handler;

import com.zhonghui.common.core.constant.FwConstants;
import com.zhonghui.common.core.exception.BusinessException;
import com.zhonghui.common.core.exception.DataOrCheckException;
import com.zhonghui.common.core.exception.TokenCheckException;
import com.zhonghui.common.core.result.FwResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.security.InvalidParameterException;

/**
 * @description 异常处理
 * @author zhonghui
 * @date 2020-12-06
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(NoHandlerFoundException.class)
    public FwResult handleNotFound(HttpServletRequest request, WebRequest webRequest) {
        return FwResult.urlNotFoundMsg("Not Found");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public FwResult handleValidationExceptions(MethodArgumentNotValidException ex, WebRequest webRequest) {
        return FwResult.validationMsg("Validation error");
    }

    @ExceptionHandler(TokenCheckException.class)
    public FwResult handleUnauthorized(TokenCheckException ex, WebRequest webRequest) {

        return FwResult.tokenFailedMsg("Unauthorized");
    }

    @ExceptionHandler({DataOrCheckException.class })
    public FwResult handleDataOrCheckException(DataOrCheckException ex, WebRequest webRequest) {
        return FwResult.validationMsg("参数错误");
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({BusinessException.class})
    public FwResult businessException(BusinessException e) {
        log.error("BusinessException 异常",e);
        return FwResult.failedMsg("系统内部错误,请稍后重试");
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex .
     * @return .
     */
    @ExceptionHandler(value = Exception.class)
    public FwResult errorHandler(Exception ex, HttpServletRequest request)
    {
        if (null != request) {
            StringBuilder strBuild = new StringBuilder();
            strBuild.append("global catch reqUrl:");
            strBuild.append(request.getRequestURI());
            if (HttpMethod.GET.matches(request.getMethod())) {
                strBuild.append(" queryString:");
                strBuild.append(request.getQueryString());
            }
            log.error(strBuild.toString(), ex);
            return FwResult.failedMsg("系统内部错误,请稍后重试");
        }
        else
        {
            log.error("global catch", ex);
            return FwResult.failedMsg("系统内部错误,请稍后重试");
        }
    }

}
